package com.dreamwhale.machinery.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.dreamwhale.machinery.entity.InfoCollect;
import com.dreamwhale.machinery.dao.InfoCollectMapper;
import com.dreamwhale.machinery.service.InfoCollectService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 资讯收藏 服务实现类
 * </p>
 *
 * @author Author
 * @since 2020-06-08
 */
@Service
public class InfoCollectServiceImpl extends ServiceImpl<InfoCollectMapper, InfoCollect> implements InfoCollectService {

    @Resource
    private InfoCollectMapper infoCollectMapper;

    @Override
    public int insertInfoCollect(InfoCollect infoCollect) {
        infoCollect.setCreateTime(LocalDateTime.now());
        return infoCollectMapper.insert(infoCollect);
    }

    @Override
    public int delInfoCollects(InfoCollect infoCollect) {
        QueryWrapper<InfoCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", infoCollect.getUserId()).eq("type", infoCollect.getType()).eq("info_id", infoCollect.getInfoId());
        return infoCollectMapper.delete(queryWrapper);
    }

    @Override
    public List<InfoCollect> selectInfoCollect(Integer userId,Integer type) {
        QueryWrapper<InfoCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("type",type);
        return infoCollectMapper.selectList(queryWrapper);
    }

    @Override
    public int selectInfoCollectCount(String userId, Integer infoId, Integer accessType) {
        QueryWrapper<InfoCollect> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).eq("info_id", infoId).eq("type", accessType);
        List<InfoCollect> list = infoCollectMapper.selectList(queryWrapper);
        if (list.size() > 0) {
            return 1;
        }
        return 0;
    }
}
